<?php
/*
zmienne przekazywane przez $_GET: 
- tag (tag jaki chcemy dodac, ew. no dla braku wyboru)
- close (=1, jeśli okno ma być zamknięte)
- form (adres pola, np. opener.document.nazwaformularza.nazwapola)
- nodir (=1, do nazwy pliku NIE zostanie dodana ścieżka)
- cmd = {image, dl, rank}
*/

//start session, local variables, ect.
include('includes/init.php');

if(!perms_check('admin', 'panel')) {
  echo('<html><body onload="javscript:window.close();"></body></html>');
  exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=<?echo($conf['html_character_set']);?>" />
<link rel="stylesheet" href="<?echo($GLOBALS['theme_path']);?>css.css" type="text/css" />
<link rel="stylesheet" href="<?echo($GLOBALS['theme_path']);?>basic.css" type="text/css" />
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" class="tabelka2">
<script type="text/javascript">
<!--
var ol_textfont = "Verdana";
var ol_fgcolor = "#FFFFFF";
var ol_bgcolor = "#000000";
var ol_textcolor = "#000000";
var ol_capcolor = "";
var ol_closecolor = "";
var ol_captionfont = "";
var ol_offsetx = 15;
var ol_offsety = 15;
var ol_vauto = 1;
var ol_hauto = 1;
-->
</script>
<div id="overdiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
<script language="javascript" type="text/javascript" src="java/overlib.js">
<!-- overLIB (c) Erik Bosrup -->
</script>
<script language="javascript" type="text/javascript" src="java/jscript.js"></script>
<script language="javascript" type="text/javascript">
<!--
<?php
if($_GET['tag']!='no')
{ if(!$_GET['tag']) $_GET['tag']='img_gal';
?>
var tag='<?echo($_GET['tag']);?>';
var align='=left';
<?} else {?>
var tag='';
var align='';
<?}?>

function update(file)
{
	if(tag) {
		if(align)
			file = '['+tag+''+align+']'+file+'[/'+tag+']';
		else
			file = '['+tag+']'+file+'[/'+tag+']';
		
		return file;
	} else 
		<?if($_GET['cmd']=='rank') {?>
		<?echo $_GET['form'];?>.value=file+"\n";
		<?}else{?>
		<?echo $_GET['form'];?>.value+=file+"\n";
		<?}?>
}
//-->
</script>

   
	<table border="0" width="100%" class="tabelka2" align="center">
<?php
if(is_module_installed($_GET['module']) && !$_GET['dir']){
	if(file_exists($conf['images_dir'].$_GET['module'].'/')) {
		$_GET['dir']=$_GET['module'].'/';
		$dir_parent=$_GET['module'].'/';
	}
	
	if($_GET['id'] && file_exists($conf['images_dir'].$_GET['dir'].$_GET['id'].'/'))
		$_GET['dir'].=$_GET['id'].'/';
	else {
		$db=new dbquery;
		$db->query("SHOW TABLE STATUS LIKE '".$conf['prefix'].$_GET['module']."';") or $db->err(__FILE__, __LINE__);
		
		$subdir=$db->fetch_object();
		$subdir=$subdir->Auto_increment;
		
		if(file_exists($conf['images_dir'].$_GET['dir'].$subdir.'/'))
			$_GET['dir'].=$subdir.'/';
	}
}

if($_GET['cmd']=='image' && !$_GET['action']) {
?>
	<tr valign="top">
		<td>
    
			<table>
			<tr>
			  <td><input type="radio" name="what" onchange="javascript:document.location.href='<?echo($_SERVER['REQUEST_URI']);?>&amp;action=url&amp;tag=img'" /></td>
			  <td><?echo($lang['image_url']);?></td>
			</tr>
			<tr>
			  <td><input type="radio" name="what" onchange="javascript:document.location.href='<?echo($_SERVER['REQUEST_URI']);?>&amp;action=local'" /></td>
			  <td><?echo($lang['image_local']);?></td>
			</tr>
			</table>

		</td>
	</tr>
<?php
}
elseif($_GET['cmd']=='image' && $_GET['action']=='url'){

?>
	<tr>
	  <td>URL: <input type="text" size="20" class="button" id="url" /></td>
	</tr>
	<tr>
	  <td><?echo($lang['admin_align']);?> <select class="button" id="align"><option value=""><?echo($lang['none']);?></option><option value="=right">right</option><option value="=left" selected>left</option><option value="=center">center</option></select></td>
	</tr>
	<tr>
	  <td><?echo($lang['image_new_window']);?> <input id="new_window" type="checkbox" onchange="javascript:if(document.getElementById('new_window').checked==1) tag='img_ext'; else tag='img';" /></td>
	</tr>
	<tr>
	  <td><input type="button" value="<?echo($lang['admin_add']);?>" onclick="add_content('['+tag+document.getElementById('align').options[document.getElementById('align').selectedIndex].value+']'+document.getElementById('url').value+'[/'+tag+']', <?echo($_GET['form']);?>); return parent.nd();" class="button" id="url" /></td>
	</tr>
<?php
}
elseif(($_GET['cmd']=='image' && $_GET['action']=='local') or $_GET['cmd']=='rank' or $_GET['cmd']=='dl') {
  $path_options=NULL;
  
  if(!$_GET['dir'] or $_GET['dir']=='/') {
	if($_GET['cmd']=='dl')
		dwl_dir_list($dir_parent, 1, 'new');
	else
		dir_list($dir_parent, 1, 'new');
  } elseif(file_exists($conf['images_dir'].$_GET['dir'].'/')) {
		$path_options='<option value="/">../</option>';
		$path_options.='<option value="'.$_GET['dir'].'">'.$_GET['dir'].'</option>';
  }
  
  if($path_options) {
?>

	<tr>
		<td>
  
			<form name="form">
			<table>
			<tr valign="top">
			  	<td>
				<?php if($_GET['nodir'] != 1){?>
			  	<select onchange="javascript:location.href='<?echo($_SERVER['REQUEST_URI']);?>&amp;dir='+this.form.dir.options[this.form.dir.selectedIndex].value+''; return false;" name="dir" class="button" size="6">
				<?php echo($path_options); ?>
				</select>
				<?php } ?>
				</td>
				<?if($_GET['tag']!='no') {?>
				<td>
				Tag:<br />
				<select onchange="javascript:tag=document.form.tag_.options[document.form.tag_.selectedIndex].value; return true;" name="tag_" class="button">
				<option value="img_gal">img_gal</option>
				<option value="img_min">img_min</option>
				<option value="img">img</option>
				</select>
				<br /><br />
				<?echo($lang['admin_align']);?><br />
				<select class="button" name="align_" onchange="javascript:align=document.form.align_.options[document.form.align_.selectedIndex].value; return true;"><option value=""><?echo($lang['none']);?></option><option value="=right">right</option><option value="=left" selected>left</option><option value="=center">center</option></select>
				</td>
				<?}?>
			</tr>
			</table>
			</form>
		</td>
	</tr>
    <tr>
		<td>
			<table align="center" width="100%" border="0">
	
  <?php

	switch($_GET['cmd']) {
		case 'dl':
			$_GET['dir'] = $conf['files_dir'].$_GET['dir'];
			break;
		case 'rank':
			$_GET['dir'] = $conf['images_dir'].$_GET['dir'];
			break;
		default:
			$_GET['dir'] = $conf['images_dir'].$_GET['dir'];
			break;
	}

	if($d = opendir($_GET['dir'])); {
		while($file = readdir($d)) {
			if(!is_dir($_GET['dir'].'/'.$file)) {
				$files[] = $file;
			}
		}
	}

	if(is_array($files))
		sort($files);

	for($i = 0; $i < count($files); $i++) {
		$overlib='';
	  
		$ext = end($foo=explode('.', $files[$i]));
		if(($ext != $files[$i]) and file_exists('admin/gfx/icons/icon_'.$ext.'.gif'))
			$icon = $ext;
		else
			$icon = 'unknown';
  

		$dir_ = str_replace($conf['files_dir'], '', $_GET['dir']);
		$dir_ = str_replace($conf['images_dir'], '', $dir_);
		$dir_ = str_replace($conf['rank_dir'], '', $dir_);
    
		if($_GET['cmd']=='image') {
			$db=new dbquery;
			$db->query("SELECT * FROM $conf[prefix]images WHERE path='$dir_' && name='$files[$i]'") or $db->err(__FILE__, __LINE__);
			$img=$db->fetch_object();
			$overlib = 'onmouseover="return overlib(\'<iframe width=\\\''.$img->minx.'\\\' height=\\\''.$img->miny.'\\\' src=\\\''.$conf['images_min_dir'].md5($img->path.$img->name).'.'.$ext.'\\\' marginwidth=\\\'0\\\' marginheight=\\\'0\\\' scrolling=\\\'NO\\\' frameborder=\\\'0\\\'>no iframe support</iframe>\', MOUSEOFF, WRAP);" onmouseout="return nd();"';
		}
    
		if($_GET['nodir'] == 1)
			$adr = $files[$i];
		elseif($_GET['cmd'] == 'dl')
			$adr = $dir_.$files[$i];
		else
			$adr = $dir_.$files[$i];

		echo('<tr>
			<td class="row_'.((($i+1)%2)+1).'" width="20"><a href="#" '.$overlib.'><img src="admin/gfx/icons/icon_'.$icon.'.gif" alt="" /></a></td>
			<td class="row_'.((($i+1)%2)+1).'" align="left"><a href="#" onClick="');
		if($_GET['tag']!='no')
			echo "add_content(update('$adr'), ".$_GET['form']."); ";
		else
			echo "update('$adr');";
		if($_GET['close'] == 1)	echo "opener.focus(); window.close();";
		echo('">'.$files[$i].'</a></td>
			<td class="row_'.((($i+1)%2)+1).'" width="30" align="center"><a href="index.php?module=admin&amp;action=images&amp;cmd=img_edit&amp;dir='.$dir_.'&amp;img='.$files[$i].'" target="_blank">'.$lang['admin_short_edit'].'</a> <a href="index.php?module=admin&amp;action=images&amp;cmd=img_rm&amp;dir='.$dir_.'&amp;img='.$files[$i].'" target="_blank">'.$lang['admin_short_delete'].'</a></td>
		</tr>');
	}
  } // if path_options?>
</table></td></tr>
<?php
}
?>
		<tr>
			<td colspan="3"><a href="javascript:history.go(-1);"><?echo($lang['back']);?></a></td>
		</tr>  
</table>
</body>
</html>
